package com.javatest;

import java.util.Scanner;

/**
 * @author LL
 * 题目：输入一行字符，分别统计出其英文字母、空格、数字和其它字符的个数。
 * @create 2020-12-10 10:59
 */
public class Subject07 {

    public static void classify(String a){
        char[] chars = a.toCharArray();
        int num1 = 0;//字母
        String num1Str = "";
        int num2 = 0;//数字
        String num2Str = "";
        int num3 = 0;//空格
        String num3Str = "";
        int num4 = 0;//其他
        String num4Str = "";
        for (int i = 0; i < chars.length; i++) {
            int ascii = (int)chars[i];
            if (48 <= ascii && ascii <= 57) {
                num2+=1;
                num2Str = num2Str + chars[i] + " ";
            }else if (ascii == 32) {
                num3+=1;
                num3Str = num3Str + chars[i] +" ";
            }else if ((65 <= ascii && ascii <= 90)||(97 <= ascii && ascii <= 122)) {
                num1+=1;
                num1Str = num1Str + chars[i] + " ";
            }else{
                num4+=1;
                num4Str = num4Str + chars[i] + " ";
            }
        }
        System.out.println("字母个数："+num1+"  --字母为："+num1Str);
        System.out.println("数字个数："+num2+"  --数字为："+num2Str);
        System.out.println("空格个数："+num3+"  --空格为："+num3Str);
        System.out.println("其他个数："+num4+"  --其他为："+num4Str);

    }

    public static void main(String[] args) {
        System.out.println("请输入一行字符：");
        Scanner sc = new Scanner(System.in);
        sc.useDelimiter("\n");//"\n" 换行符（回车）作为输入的分隔符
        String next = sc.next();
        classify(next);
    }
}
